const workbench = {
    route: '/workbench',
    name: 'workbench',
    label: '工作台',
    labelEn: 'workbench',
};
const common = {
    state: {
        tabs: [workbench],
        activeIndex: '/workbench',
        dialogWidth600: window.screen.width * 0.42 > 600 ? window.screen.width * 0.42 + 'px' : '600px',
        dialogWidth1000: window.screen.width * 0.69 > 1000 ? window.screen.width * 0.69 + 'px' : '1000px',
        dialogWidth1200: window.screen.width * 0.83 > 1200 ? window.screen.width * 0.83 + 'px' : '1200px',
    },
    getters: {
        tabs: state => state.tabs,
    },
    actions: {
        delTab({ commit, state }, route) {
            commit('delete_tabs', route);
        }
    },
    mutations: {
        add_tabs(state, data) {
            let isExist = state.tabs.some(e => {
                return e.route == data.route;
            });
            if (!isExist) state.tabs.push(data);
        },
        delete_tabs(state, route) {
            let index = 0;
            for (let tab of state.tabs) {
                if (tab.route === route) {
                    break;
                }
                index++;
            }
            state.tabs.splice(index, 1);
        },
        // 除指定路由外，删除其他所有
        delete_tabs_exclude(state, routes) {
            state.tabs = state.tabs.filter(tab => routes.includes(tab.route));
        },
        set_active_index(state, index) {
            if (index) state.activeIndex = index;
            else state.activeIndex = workbench.route;
            // localStorage存储当前路由
            localStorage.setItem("setActiveIndex", state.activeIndex);
        },
        clear_tab_info: state => {
            state.tabs = [workbench];
            state.activeIndex = '/workbench';
            localStorage.removeItem('tabs');
        }
    }
};

export default common;
